package org.jivesoftware.spark.crowd.impl;

import java.util.ArrayList;
import java.util.List;

import org.jivesoftware.smack.PacketCollector;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.filter.AndFilter;
import org.jivesoftware.smack.filter.FromContainsFilter;
import org.jivesoftware.smack.filter.OrFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.filter.PacketIDFilter;
import org.jivesoftware.smack.filter.PacketTypeFilter;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.provider.IQProvider;
import org.jivesoftware.smack.provider.ProviderManager;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.spark.SparkManager;
import org.jivesoftware.spark.crowd.packet.DiscoverCrowds;
import org.jivesoftware.spark.util.ModelUtil;
import org.xmlpull.v1.XmlPullParser;

public class TestCls implements PacketListener{
	public void test() {
		
//		addListener();
//		sendMessage("just for test : )");
//		addIQListener();
		sendIQ();
		
	}
	private void sendIQ(){
		ProviderManager.getInstance().addIQProvider("a", "abcdef", new IQProvider(){
			@Override
			public IQ parseIQ(XmlPullParser parser) throws Exception {
				TestIQ testIQ = new TestIQ();
				TestIQ.Sub sub = new TestIQ.Sub();
				
				boolean done = false;
				while(!done){
					 int eventType = parser.next();
					 
					 if(eventType == XmlPullParser.START_TAG){
						 if(parser.getName().equals("test")){
							 sub.setName(parser.getAttributeValue("", "name"));
							 testIQ.addItem(sub);
						 }
					 }else if(eventType == XmlPullParser.END_TAG){
						 if(parser.getName().equals("a"))
							 done = true;
					 }
				}
	
				return testIQ;
			}
			
		});
		
		
		IQ IQPacket = new IQ(){

			@Override
			public String getChildElementXML() {
				// TODO Auto-generated method stub
				return "<a xmlns=\"abcdef\"/>";
			}
			
		};
//		IQPacket.setFrom("liyuan@127.0.0.1");
		IQPacket.setTo("127.0.0.1");
		IQPacket.setType(IQ.Type.GET);
		
		
		
//		SparkManager.getConnection().sendPacket(IQPacket);
		PacketFilter filter = new PacketIDFilter(IQPacket.getPacketID());
		PacketCollector pc = SparkManager.getConnection().createPacketCollector(filter);
		SparkManager.getConnection().sendPacket(IQPacket);
		
		
		IQ result = (IQ)pc.nextResult(3000);
		
		pc.cancel();
		if(result == null){
			System.out.println("result = null!");
			return;
		}
		System.out.println(result.toXML());
		
		
		
	}
	

	private void addListener() {
		  PacketFilter fromFilter = new FromContainsFilter("127");
		  PacketFilter orFilter = new OrFilter(new PacketTypeFilter(Presence.class), new PacketTypeFilter(Message.class));
	        PacketFilter andFilter = new AndFilter(orFilter, fromFilter);
		SparkManager.getConnection().addPacketListener(this, andFilter );
	}
	private void addIQListener(){
		DiscoverCrowds.DiscoverCrowdsProvider.getInstance();
		
		
		 PacketFilter fromFilter = new FromContainsFilter("crowd.");
		  PacketFilter typeFilter = new PacketTypeFilter(IQ.class);
	        PacketFilter andFilter = new AndFilter(typeFilter, fromFilter);
		SparkManager.getConnection().addPacketListener(this, andFilter );
	}
	private void sendMessage(String text) {
		final Message message = new Message();
		message.setThread(StringUtils.randomString(12));
		text = text.replaceAll("[\\u0001-\\u0008\\u000B-\\u001F]", "");
		message.setBody(text);
		if (!ModelUtil.hasLength(text)) {
			return;
		}
		message.setType(Message.Type.chat);
//		message.setTo("crowdName@crowd.127.0.0.1");
		
		message.setTo("guolf@127.0.0.1");
//		message.setFrom(SparkManager.getSessionManager().getJID());
		message.setFrom("qun01@crowd.127.0.0.1");
		SparkManager.getConnection().sendPacket(message);

	}

	@Override
	public void processPacket(Packet packet) {
		if(packet instanceof Message){
			System.out.println("from : "+packet.getFrom()+"   to : " + packet.getTo());
			System.out.println("body : " + ((Message)packet).getBody());
		}else if(packet instanceof IQ){
			IQ iqReceived = (IQ)packet;
			System.out.println(iqReceived.toXML());
			
		}
		
	}
	private static class TestIQ extends IQ{
		List<Sub> subs = new ArrayList<Sub>();
		@Override
		public String getChildElementXML() {
			String child = "\n<a xmlns=\"abcdef\"/>\n";
			for(Sub s : subs){
				child = child + s.toXML();
			}
			child += "</a>";
			return  child;
		}
		public void addItem(Sub sub){
			subs.add(sub);
		}
		public static class Sub{
			private String name;
			public String getName() {
				return name;
			}
			public void setName(String name) {
				this.name = name;
			}
			
			public String toXML(){
				return "<test name=\"" + name + "\"/>\n";
			}
		}
		
	}
	
}
